package backend

import(
//	"fmt"
	"gopkg.in/mgo.v2/bson"
	//"questionnaire-gen"
)
const(
	coll_results string = "tplResults"
)

//Return template by Unique ID
func GetTemplate(uid string) (*Template, error){
	s, c := GetColl(nil, coll_results) 
	//s, c := GetSessionColl("testData") 
	defer s.Close()
	t := &Template{}

	err := c.Find(bson.M{"uid": uid}).One(t)
	if err != nil {t = nil;}
	return t, err
}

func InsertTemplate(tpl *Template) error{
	s, c := GetColl(nil, coll_results) 
	defer s.Close()
	
	tpl.Id = bson.NewObjectId()
	return c.Insert(tpl)
}

//update template with Id, if ID is not defined a new template will be inserted
func UpdateTemplate(tpl *Template) error{
	s, c := GetColl(nil, coll_results) 
	defer s.Close()
	if tpl.Id != "" {
		err := c.UpdateId(tpl.Id, tpl)
		return err
	}
	//insert new template
	tpl.Id = bson.NewObjectId()
	return c.Insert(tpl)
}



